package com.yunwei;

import android.app.Application;
import android.content.Context;

import com.yunwei.dao.DaoMaster;
import com.yunwei.dao.DaoSession;
import com.yunwei.dao.NoteDao;
import com.yunwei.dao.SQLiteOpenHelper;
import com.yunwei.service.NoteService;

/**
 * Created by weijianxing on 9/16/15.
 */
public class BaseApplication extends Application {

    private static BaseApplication instance;


    private static DaoMaster daoMaster;

    private static DaoSession daoSession;

    private static NoteService noteService;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    public static BaseApplication getInstance() {
        return instance;
    }

    public static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            DaoMaster.OpenHelper helper = new SQLiteOpenHelper(context, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }


    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            daoSession = getDaoMaster(context).newSession();
        }

        return daoSession;
    }


    public static NoteDao getNoteDao(Context context) {
        return getDaoSession(context).getNoteDao();
    }

    public static NoteService getNoteService(Context context) {
        if (noteService == null) {
            noteService = new NoteService(getNoteDao(context));
        }

        return noteService;
    }


}
